package com.premadefood.controller;

import com.premadefood.common.BaseResponse;
import com.premadefood.common.ErrorCode;
import com.premadefood.domain.*;
import com.premadefood.domain.request.UserLoginRequest;
import com.premadefood.domain.request.UserRegisterRequest;
import com.premadefood.domain.vo.GoodsVO;
import com.premadefood.exception.BusinessException;
import com.premadefood.service.UserService;
import com.premadefood.utils.ResultUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;

import static com.premadefood.constant.UserConstant.USER_LOGIN_STATE;


@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    @PostMapping("/login")
    @ApiOperation("用户登录")
    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest,
                                        HttpServletRequest request){
        if(userLoginRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"登录参数为空");
        }
        String account = userLoginRequest.getAccount();
        String password = userLoginRequest.getPassword();
        if(StringUtils.isAnyBlank(account,password)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号，密码不能为空");
        }

        User user = userService.userLogin(account, password, request);
        return ResultUtils.success(user);
    }



    @PostMapping("/register")
    @ApiOperation("用户注册")
    public BaseResponse<User> userRegister(@RequestBody UserRegisterRequest userRegisterRequest,
                                           HttpServletRequest request){
        if(userRegisterRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"注册参数为空");
        }
        String account = userRegisterRequest.getAccount();
        String password = userRegisterRequest.getPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if(StringUtils.isAnyBlank(account,password,checkPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号，密码，确认密码不能为空");
        }
        User user = userService.userRegister(account, password, checkPassword,request);
        return ResultUtils.success(user);
    }

    /**
     * 点击进入修改个人信息
     * @param request
     * @return
     */
    @GetMapping("/current")
    @ApiOperation("点击进入修改个人信息,展示当前用户的各信息")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request){
        User currentUser = (User)request.getSession().getAttribute(USER_LOGIN_STATE);
        if(currentUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN,"用户未登录");
        }
        Integer userId = currentUser.getId();
        User user = userService.getById(userId);
        User safeUser = userService.getSafeUser(user);
        return ResultUtils.success(safeUser);
    }

    /**
     * 点击订单详情
     * @param request
     * @return
     */
    @ApiOperation("点击订单详情")
    @GetMapping("/order")
    public BaseResponse<List<Userorder>> getOrderInfo(@RequestParam String orderStatus
            ,HttpServletRequest request){
        if(StringUtils.isBlank(orderStatus)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"订单状态不能为空");
        }
        List<Userorder> orderInfo = userService.getOrderInfo(orderStatus,request);
        return ResultUtils.success(orderInfo);
    }


    @ApiOperation("查看当前用户收藏的商品")
    @GetMapping("/favorite/goods")
    public BaseResponse<List<GoodsVO>> seeFavoriteGoods(HttpServletRequest request){
        List<GoodsVO> goodsVOList = userService.seeFavoriteGoods(request);
        return ResultUtils.success(goodsVOList);
    }

    @ApiOperation("查看当前用户的商品浏览历史")
    @GetMapping("/history/goods")
    public BaseResponse<List<GoodsVO>> seeHistoryGoods(HttpServletRequest request){
        List<GoodsVO> goodsVOList = userService.seeHistoryGoods(request);
        return ResultUtils.success(goodsVOList);
    }


    /**
     * 查看关注了的店铺名
     * @return
     */
    @ApiOperation("查看关注了的店铺名")
    @GetMapping("/followedstore")
    public BaseResponse<List<String>> seeFollowedStore(HttpServletRequest request){
        List<String> followedStore = userService.seeFollowedStore(request);
        return ResultUtils.success(followedStore);
    }

    /**
     * 在关注了的店铺名中点击店铺名称查看该店铺拥有的产品
     *
     */
    @ApiOperation("在关注了的店铺名中点击店铺名称查看该店铺拥有的产品")
    @GetMapping("/followedstore/goods")
    public BaseResponse<List<GoodsVO>> getStoreGoods(String storeName){
        if(StringUtils.isBlank(storeName)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"店铺名称不能为空");
        }
        List<GoodsVO> storeGoods = userService.getStoreGoods(storeName);
        return ResultUtils.success(storeGoods);
    }


    /**
     * 点击修改按钮修改用户属性
     *
     */
    @ApiOperation("点击修改按钮修改用户属性")
    @PostMapping("/update")
    public BaseResponse<Integer> updateUser(@RequestBody User user,HttpServletRequest request){
        //1.校验参数是否为空
        if(user == null)
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        int result = userService.updateUser(user,loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 点击消息按钮，进入消息中心
     */
    @ApiOperation("点击消息按钮，进入消息中心")
    @GetMapping("/message/center")
    public BaseResponse<List<MsgCenter>> enterMessageCenter(HttpServletRequest request){
        List<MsgCenter> msgCenterList = userService.enterMessageCenter(request);
        return ResultUtils.success(msgCenterList);
    }


    /**
     * 点击对应聊天室,对应的聊天室中的消息
     */
    @ApiOperation("点击对应聊天室,对应的聊天室中的消息")
    @GetMapping("/message")
    public BaseResponse<List<Msg>> getMsg(@RequestParam Integer centerId,HttpServletRequest request){
        if(centerId <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"聊天室id应为正数");
        }
        List<Msg> msgList = userService.getMsg(centerId,request);
        return ResultUtils.success(msgList);
    }

}
